package twilight.companion.entity.ai;

import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.goal.target.TargetGoal;
import net.minecraft.world.entity.ai.targeting.TargetingConditions;
import twilight.companion.entity.MiniYetiEntity;

import java.util.EnumSet;

public class MiniYetiOwnerAttackTargetGoal extends TargetGoal {
    private final MiniYetiEntity yeti;
    private LivingEntity ownerLastTarget;
    private int timestamp;

    public MiniYetiOwnerAttackTargetGoal(MiniYetiEntity yeti) {
        super(yeti, false);
        this.yeti = yeti;
        this.setFlags(EnumSet.of(Flag.TARGET));
    }

    @Override
    public boolean canUse() {
        if (!this.yeti.isTame()) {
            return false;
        }

        LivingEntity owner = this.yeti.getOwner();
        if (owner == null) {
            return false;
        }

        LivingEntity ownerTarget = owner.getLastHurtMob();
        int ownerTargetTimestamp = owner.getLastHurtMobTimestamp();

        if (ownerTarget != null && (ownerTargetTimestamp != this.timestamp || ownerTarget != this.yeti.getTarget())) {
            this.ownerLastTarget = ownerTarget;
            this.timestamp = ownerTargetTimestamp;
            if (this.canAttack(ownerTarget, TargetingConditions.DEFAULT)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean canContinueToUse() {
        if (!this.yeti.isTame()) {
            return false;
        }

        LivingEntity owner = this.yeti.getOwner();
        if (owner == null) {
            return false;
        }

        LivingEntity ownerTarget = owner.getLastHurtMob();
        if (ownerTarget != this.ownerLastTarget) {
            return false;
        }
        return this.ownerLastTarget != null && this.ownerLastTarget.isAlive();
    }

    @Override
    public void start() {
        this.yeti.setTarget(this.ownerLastTarget);
        super.start();
    }

    @Override
    public void tick() {
        super.tick();

        if (this.yeti.isTame() && this.yeti.getOwner() != null) {
            LivingEntity owner = this.yeti.getOwner();
            LivingEntity ownerTarget = owner.getLastHurtMob();
            int ownerTargetTimestamp = owner.getLastHurtMobTimestamp();
            if (ownerTarget != null && ownerTarget != this.ownerLastTarget && 
                ownerTargetTimestamp != this.timestamp) {
                this.ownerLastTarget = ownerTarget;
                this.timestamp = ownerTargetTimestamp;
                if (this.canAttack(ownerTarget, TargetingConditions.DEFAULT)) {
                    this.yeti.setTarget(ownerTarget);
                }
            }
        }
    }

    @Override
    protected boolean canAttack(LivingEntity potentialTarget, TargetingConditions targetPredicate) {
        if (potentialTarget == this.yeti.getOwner()) {
            return false;
        }

        if (potentialTarget instanceof MiniYetiEntity otherYeti && 
            otherYeti.isTame() && otherYeti.getOwner() == this.yeti.getOwner()) {
            return false;
        }
        return super.canAttack(potentialTarget, targetPredicate);
    }
}